﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CROSLAND.BL.BE;
using System.Data.Common;
using System.Data;

namespace CROSLAND.DL.DALC.MSSQL
{
    public class JerarquiaDALC : BaseDALC, IJerarquiaDALC
    {       
        #region IJerarquiaDALC Members

        public List<JerarquiaBE> listar(int nivel)
        {
            List<JerarquiaBE> entitys = new List<JerarquiaBE>();

            DbCommand command = dbProvider.GetStoredProcCommand("USPR_LISTAR_JERARQUIAS");

            dbProvider.AddInParameter(command, "P_STUFE", DbType.Int32, nivel);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    JerarquiaBE entity = new JerarquiaBE();

                    entity.Jerarquia = dr["PRODH"].ToString();
                    entity.Descripcion = dr["VTEXT"].ToString();
                    entity.Nivel = Convert.ToInt32(dr["STUFE"].ToString());

                    entitys.Add(entity);
                }
            }

            return entitys;           
        }

        public List<JerarquiaBE> Buscar(int pNivel, string pJerarquia)
        {
            List<JerarquiaBE> entitys = new List<JerarquiaBE>();

            DbCommand command = dbProvider.GetStoredProcCommand("USPR_BUSCAR_JERARQUIAS_HIJAS");

            dbProvider.AddInParameter(command, "P_PRODH", DbType.String, pJerarquia.Trim());
            dbProvider.AddInParameter(command, "P_STUFE", DbType.Int32, pNivel);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    JerarquiaBE entity = new JerarquiaBE();

                    entity.Jerarquia = dr["PRODH"].ToString();
                    entity.Descripcion = dr["VTEXT"].ToString();
                    entity.Nivel = Convert.ToInt32(dr["STUFE"].ToString());

                    entitys.Add(entity);
                }
            }

            return entitys;           
                                
        }

        #endregion
    }
}
